package com.example.transaction.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.transaction.common.vo.Result;
import com.example.transaction.sys.entity.Favorites;
import com.example.transaction.sys.entity.Item;
import com.example.transaction.sys.entity.LeavedMessage;
import com.example.transaction.sys.service.IFavoritesService;
import com.example.transaction.sys.service.ILoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/favorites")
public class FavoritesController {
    @Autowired
    private IFavoritesService favoritesService;

    /**
     * 查找用户喜欢的物品id
     * @param userID
     * @return
     */
    @Cacheable(value = "favorites", key = "'allfavorites'")
    @GetMapping("/{userID}")
    public Result<List<Favorites>> getFavoritesByUserId(@PathVariable("userID")String userID){
        LambdaQueryWrapper<Favorites> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(userID),Favorites::getUserID,userID);
        List<Favorites> favorites=favoritesService.list(wrapper);
        return Result.success(favorites);
    }

    /**
     * 增加收藏商品
     * @param favorites
     * @return
     */
    @CacheEvict(value = "favorites", allEntries = true)
    @PostMapping("/addFavorites")
    public Result<?>addItem(@RequestBody Favorites favorites){
        try {
            favoritesService.save(favorites);
        }catch (Exception e){
            return Result.fail("新增失败");
        }
        return Result.success("新增收藏成功");
    }


    /**
     * 删除收藏商品
     * @param favoritesID
     * @return
     */
    @CacheEvict(value = "favorites", allEntries = true)
    @DeleteMapping("/{favoritesID}")
    public Result<Item>deleteItemById(@PathVariable("favoritesID")Integer favoritesID){
        favoritesService.removeById(favoritesID);
        return Result.success("删除收藏成功");
    }

    /**
     * 查询所有收藏，或者按条件查询(名称模糊搜索，价格区间限定)
     * @param
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Cacheable(value = "favorites", key = "'allfavorites'+#userID+#itemID+#pageNo+#pageSize")
    @GetMapping("/list")
    public Result<Map<String,Object>>getFavoritesList(@RequestParam(value = "userID")Integer userID,
                                                      @RequestParam(value = "itemID",required = false)String itemID,
                                                 @RequestParam(value = "pageNo")Long pageNo,
                                                 @RequestParam(value = "pageSize")Long pageSize) {
        LambdaQueryWrapper<Favorites> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorites::getUserID, userID);
        if(StringUtils.hasLength(itemID)){
            wrapper.eq(Favorites::getItemID,itemID);
        }
        Page<Favorites> page = new Page<>(pageNo, pageSize);
        favoritesService.page(page, wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());
        return Result.success(data);
    }
}
